/***********************************************************************************
**  
* @copyright (c) 2010-2019, ChengDu Duyer Technology Co., LTD. All Right Reserved.
*
************************************************************************************/
/**
* @file	    mjs_sysver_action.cpp
* @version     
* @brief      
* @author   duye
* @date	    2016-12-30
* @note 
*
* 1. 2016-12-30 duye Created this file
*/

#include <string>
#include <duye_type.h>
#include <duye_logger.h>
#include <mj_net_protocol.h>
#include <mjs_inner_msg.h>
#include <mjs_mysql.h>
#include <mjs_reqer.h>
#include <mjs_sysver_action.h>

static const int8* DUYE_LOG_PREFIX = "mangjings.action.sysver";

MJSSysVerAction::MJSSysVerAction() {}
MJSSysVerAction::~MJSSysVerAction() {}

bool MJSSysVerAction::onAction(MJMsgAction* action, MJSReqer* reqer) {
	if (!action || !reqer) {
		DUYE_ERROR("action == NULL || reqer == NULL");
		return false;
	}

	if (action->getName() != "sysver") {
		DUYE_ERROR("action_name != 'sysver");
		return false;		
	}

	std::string sys_name = action->getValue("sys_name");
	std::string sys_ver = action->getValue("sys_version");
	std::string mjc_ver = action->getValue("mjc_version");
	std::string sys_info = sys_name + "-" + sys_ver;

	DUYE_TRACE("sys_name=%s sys_ver=%s mjc_ver=%s", sys_name.c_str(), sys_ver.c_str(), mjc_ver.c_str());

	// update server_info
	std::string sql = "UPDATE server_info SET sys_info = ?, client_ver = ? WHERE user = ?";
	int32 rows = MJSMysql::ins().update(sql, MJSMysql::Params(sys_info, mjc_ver, reqer->getUser()));
	if (rows < 0) {
		DUYE_ERROR("client:%s store sys_info(%s) mjc_ver(%s) to db failed", sys_info.c_str(), mjc_ver.c_str());
		return false;
	} else {
		DUYE_DEBUG("client:%s store sys_info(%s) mjc_ver(%s) to db ok", sys_info.c_str(), mjc_ver.c_str());
	}

	return true;
}